import {nextTick, reactive, ref, watch, shallowRef} from "vue";
import {isEqual, cloneDeep} from "lodash-es";
import Schema from 'async-validator';

export default function useTableHooks() {
    const tableData = ref<any[]>([])
    const validator = shallowRef<any>();
    const removeRows = shallowRef<object[]>([])

    /**
     * 设置表格数据
     * @param data
     */
    const setData = (data) => {
        if (data && Array.isArray(data)) {
            data.forEach(res => {
                res.$editState = 0
            })
            tableData.value = data
        } else {
            console.warn("setData这个函数参数只能是数组")
        }

    }
    /**
     * 获取表格数据
     */
    const getData = () => {
        return tableData.value
    }
    /**
     * 设置表格字段
     * @param index
     * @param label
     * @param value
     */
    const setTableField = (index, label, value) => {
        tableData.value[index][label] = value
    }
    /**
     * 设置表格字段
     * @param index
     * @param label
     */
    const getTableField = (index, label) => {
        return tableData.value[index][label]
    }
    /**
     * 添加表格数据
     * @param row
     */
    const add = (row) => {
        if (!row) {
            console.warn("参数不能为空")
            return
        }
        if (row instanceof Object && !Array.isArray(row)) {
            row.$editState = 1
            tableData.value.push(row)
        }
    }
    /**
     * 批量添加表格数据
     */
    const addAll = (rows) => {
        if (!Array.isArray(rows)){
            console.warn("addAll函数参数必须为数组")
            return
        }
        rows.forEach(res=>{
            res.$editState = 1
        })
        tableData.value.push(...rows)

    }
    /**
     * 删除表格数据
     * @param index
     */
    const remove = (index) => {
        removeRows.value.push(tableData.value[index])
        tableData.value.splice(index, 1)
    }
    /**
     * 设置表格校验规则
     * @param validateObj
     */
    const setRules = (validateObj) => {
        validator.value = new Schema(validateObj);
    }

    /**
     * 行校验
     * @param row
     */
    const rowValidate = async (row) => {
       return new Promise((resolve, reject) => {
           validator.value.validate(row,(errors)=>{
               if (errors) {
                   row.$errorList = errors.map((r) => r.field)
                   resolve(false)
               }else{
                   resolve(true)
               }

           });
       })


    }

    /**
     * 表格的数据校验
     */
    const validate = async () => {
        if (tableData.value && tableData.value.length > 0) {
            for (const row of tableData.value) {
                let res = await rowValidate(row)
                if (!res) {
                   throw new Error("表格数据校验未通过")
                }
            }
        }
        return true
    }


    return {
        tableData,
        setData,
        getData,
        setTableField,
        getTableField,
        add,
        remove,
        setRules,
        validator,
        validate,
        addAll

    }
}


